package com.horoscope.astrology.zodiac.signs.free.fragment

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.view.animation.Animation
import android.view.animation.AnimationUtils
import android.widget.LinearLayout
import android.widget.TextView
import com.horoscope.astrology.zodiac.signs.free.R
import com.horoscope.astrology.zodiac.signs.free.activity.PreMainActivity

/**
 * 性别选择
 * */
class GenderFragment : BaseFragment() {
    private var mLinearGender: LinearLayout? = null
    private var maleSelect: TextView? = null
    private var femaleSelect: TextView? = null
    private var male: TextView? = null
    private var mReplyGender: LinearLayout? = null
    private var mReplyText: TextView? = null
    private var female: TextView? = null
    private var singleClick = true
    private var mQuestion: TextView? = null


    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        return inflater.inflate(R.layout.fragment_gender, container, false)
    }


    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        initView(view)
        inVisibilityRoot()

    }

//    fun setName(name: String) {
//        mNameStr = name
//        @SuppressLint("SetTextI18n")
//        mQuestion?.text = name + "! " + activity.getString(R.string.tips_gender_str)
//    }


    private fun initView(view: View) {
        male = view.findViewById(R.id.text_view_male)
        mQuestion = view.findViewById(R.id.text_view_content)
        female = view.findViewById(R.id.text_view_female)
        maleSelect = view.findViewById(R.id.text_view_male_select)
        femaleSelect = view.findViewById(R.id.text_view_female_select)
        mLinearGender = view.findViewById(R.id.linear_gender)
        mReplyGender = view.findViewById(R.id.linear_layout_reply_gender)
        mReplyText = view.findViewById(R.id.text_view_reply_gender)
        mRoot = view.findViewById(R.id.linear_root_gender)

        male?.setOnClickListener {
            if (singleClick) {
                val fadeOut = AnimationUtils.loadAnimation(activity, R.anim.fade_out)
                mLinearGender?.startAnimation(fadeOut)
                maleSelect?.visibility = View.VISIBLE
                val anim = AnimationUtils.loadAnimation(activity, R.anim.male_select)
                anim.setAnimationListener(object : Animation.AnimationListener {
                    override fun onAnimationRepeat(animation: Animation?) {
                    }

                    override fun onAnimationEnd(animation: Animation?) {
                        maleSelect?.clearAnimation()
                        maleSelect?.visibility = View.INVISIBLE
                        setReply()
                    }

                    override fun onAnimationStart(animation: Animation?) {
                    }

                })
                mReplyText?.text = activity.getString(R.string.label_male)
                (activity as PreMainActivity).setGender(activity.getString(R.string.label_male))
                maleSelect?.startAnimation(anim)
            }
            singleClick = false

        }
        female?.setOnClickListener {
            if (singleClick) {
                val fadeOut = AnimationUtils.loadAnimation(activity, R.anim.fade_out)
                mLinearGender?.startAnimation(fadeOut)
                femaleSelect?.visibility = View.VISIBLE
                val anim = AnimationUtils.loadAnimation(activity, R.anim.female_select)
                femaleSelect?.startAnimation(anim)
                mReplyText?.text = activity.getString(R.string.label_female_1)
                (activity as PreMainActivity).setGender(activity.getString(R.string.label_female_1))
                anim.setAnimationListener(object : Animation.AnimationListener {
                    override fun onAnimationRepeat(animation: Animation?) {
                    }

                    override fun onAnimationEnd(animation: Animation?) {
                        femaleSelect?.clearAnimation()
                        femaleSelect?.visibility = View.INVISIBLE
                        setReply()
                    }

                    override fun onAnimationStart(animation: Animation?) {
                    }

                })
            }
            singleClick = false
        }
    }

    private fun setReply() {
        val anim = AnimationUtils.loadAnimation(activity, R.anim.fade_in)
        mReplyGender?.startAnimation(anim)
        anim.setAnimationListener(object : Animation.AnimationListener {
            override fun onAnimationRepeat(animation: Animation?) {
            }

            override fun onAnimationEnd(animation: Animation?) {
                val mAnim = AnimationUtils.loadAnimation(activity, R.anim.fade_out)
                mRoot?.postDelayed({
                    mRoot?.startAnimation(mAnim)
                }, 500)

                mAnim.setAnimationListener(object : Animation.AnimationListener {
                    override fun onAnimationRepeat(animation: Animation?) {
                    }

                    override fun onAnimationEnd(animation: Animation?) {
                        if (activity is PreMainActivity) {
                            (activity as PreMainActivity).setPage(2)
                        }
                    }

                    override fun onAnimationStart(animation: Animation?) {
                    }

                })
            }

            override fun onAnimationStart(animation: Animation?) {
            }

        })
    }

//    fun fadeIn() {
//        super.fadeInRoot(mRoot)
//    }

}